// pages/songPlay/songPlay.js
import PubSub from 'pubsub-js'
import moment from 'moment'
import request from '../../utils/request'
Page({
  async getDetail(ids) {
    // 获取详情
    let res = await request({
      url: '/song/detail',
      data: {
        ids
      }
    })
    let songMsg = res.songs[0]
    wx.setNavigationBarTitle({
      title: songMsg.name,
    })
    this.setData({
      songDetail: {
        name: songMsg.name,
        songId: songMsg.id,
        img: songMsg.al.picUrl,
        singer: songMsg.ar[0].name,
        totalTime: moment(res.songs[0].dt).format('mm:ss')
      }
    })
    // 详情获取完毕，尝试获取播放地址
    let urlRes = await request({
      url: '/song/url',
      data: {
        id: ids
      }
    })
    let songUrl = urlRes.data[0].url
    this.setData({
      songUrl
    })
  },
  // 歌曲的初次播放
  fristPlay() {
    this.setData({
      isPlay: true
    })
    this.player = wx.getBackgroundAudioManager()
    this.player.src = this.data.songUrl // 播放地址 但是还需要一个title
    this.player.title = this.data.songDetail.name
  },
  // 播放/暂停
  setIsPlay() {
    const isPlay = !this.data.isPlay
    this.setData({
      isPlay
    })
    if (isPlay) {
      this.player.play()
    } else {
      this.player.pause()
    }
  },
  // 上一首/下一首
  changeSong(e) {
    PubSub.publish('type', e.target.id)
  },
  // 拖拽/点击进度条
  // setStart (e){
  //   // console.log(e, 'sc');

  //   let positionX = e.touches[0].clientX*2-e.currentTarget.offsetLeft*2
  //   console.log(e.touches[0].clientX, 'sc');
  //   console.log(positionX, 'sp');
  //   this.setData({
  //     progressWidth: positionX/500 *100 +'%'
  //   })
  // },
  setting (e){
    // console.log(e);
    let positionX = e.touches[0].clientX-e.currentTarget.offsetLeft
    console.log(e.touches[0].clientX, 'c');
    // console.log(e.currentTarget.offsetLeft, 'p');
    console.log(positionX);
    this.setData({
      progressWidth: positionX*2/500 *100 +'%'
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    songDetail: {},
    isPlay: false,
    songUrl: '',
    currentTime: '0:00',
    progressWidth: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    await this.getDetail(options.id)
    this.fristPlay()
    this.player.onPlay(() => {
      this.setData({
        isPlay: true
      })
    })
    this.player.onPause(() => {
      this.setData({
        isPlay: false
      })
    })
    PubSub.subscribe('songId', async (_, id) => {
      await this.getDetail(id)
      this.fristPlay()
    })
    this.player.onEnded(() => {
      PubSub.publish('type', 'next')
    })
    this.player.onTimeUpdate(() => {
      const progressWidth = this.player.currentTime / this.player.duration * 100 + '%'
      this.setData({
        currentTime: moment(this.player.currentTime*1000).format('mm:ss'),
        progressWidth
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})